﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Simple Segmentation
    /// </summary>
    public class SimpleSegmentation : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleSegmentation"/> class.
        /// </summary>
        public SimpleSegmentation()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleSegmentation"/> class.
        /// </summary>
        /// <param name="_simpleSegmentation">The _simple segmentation.</param>
        internal SimpleSegmentation(SimpleSegmentation _simpleSegmentation)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            int y = 0;
            int x = 0;
            List<Color> aColor = new List<Color>();
            aColor.Add(bitmap.GetPixel(0, 0));
            int nCount = 1;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            Color colorPrev = Color.Transparent;
            Color color = Color.Transparent;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    colorPrev = aColor[nCount - 1];
                    color = bitmap.GetPixel(x, y);
                    aColor.Add(color);
                    if (ColorFunctions.IsInColorTolerance(color, colorPrev, 25, 25, 25))
                        aColor.Add(color);
                    aColor.Add(color);
                    aColor.Add(color);
                    {
                        Color colorRounded = ColorFunctions.ColorComponentRound(color, MidpointRounding.AwayFromZero, 50);
                        bitmap.SetPixel(x, y, colorRounded);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new SimpleSegmentation(this);
        }
    }
}
